<!DOCTYPE html>
<html>
<head>
    <title>PlayCanvas Hardware Instancing</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
    <link rel="icon" type="image/png" href="../playcanvas-favicon.png" />
    <script src="../../build/output/playcanvas.js"></script>
    <style>
        body { 
            margin: 0;
            overflow-y: hidden;
        }
    </style>
</head>

<body>
    <!-- The canvas element -->
    <canvas id="application-canvas"></canvas>

    <!-- The script -->
    <script>
        var canvas = document.getElementById("application-canvas");

        // Create the application and start the update loop
        var app = new pc.Application(canvas);
        app.start();

        // Set the canvas to fill the window and automatically change resolution to be the same as the canvas size
        app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);
        app.setCanvasResolution(pc.RESOLUTION_AUTO);

        window.addEventListener("resize", function () {
            app.resizeCanvas(canvas.width, canvas.height);
        });

        app.scene.ambientLight = new pc.Color(0.2, 0.2, 0.2);

        // Create an Entity with a point light component
        var light = new pc.Entity();
        light.addComponent("light", {
            type: "point",
            color: new pc.Color(1, 0, 1),
            radius: 10
        });

        // Create an Entity with a camera component
        var camera = new pc.Entity();
        camera.addComponent("camera", {
            clearColor: new pc.Color(0.4, 0.45, 0.5)
        });

        // Add the new Entities to the hierarchy
        app.root.addChild(light);
        app.root.addChild(camera);

        // Move the camera back to see the cubes
        camera.translate(0, 0, 10);

        // create standard material and enable instancing on it        
        var material = new pc.StandardMaterial();
        material.onUpdateShader = function(options) {
            options.useInstancing = true;
            return options;
        };
        material.update();

        // Create a Entity with a Box model component and the instancing material
        var box = new pc.Entity();
        box.addComponent("model", {
            material: material,
            type: "box"
        });

        // add the box entity to the hierarchy
        app.root.addChild(box);

        // number of instances to render
        var instanceCount = 15;

        // store matrices for individual instances into array
        var matrices = new Float32Array(instanceCount * 16);
        var matrixIndex = 0;
    
        var pos = new pc.Vec3();
        var rot = new pc.Quat();
        var scl = new pc.Vec3(0.1, 0.1, 0.1);
        var matrix = new pc.Mat4();

        for (var i = 0; i < instanceCount; i++) {
            pos.x = i * 0.2 - instanceCount * 0.5 * 0.2;
            rot.setFromEulerAngles(i * 30, i * 50, i * 70);
            matrix.setTRS(pos, rot, scl);

            // copy matrix elements into array of floats
            for (var m = 0; m < 16; m++)
                matrices[matrixIndex++] = matrix.data[m];
        }

        // create static vertex buffer containing the matrices
        var vertexBuffer = new pc.VertexBuffer(this.app.graphicsDevice, pc.VertexFormat.defaultInstancingFormat, instanceCount, pc.BUFFER_STATIC, matrices);

        // initialise instancing using the vertex buffer on meshInstance of the created box
        var boxMeshInst = box.model.meshInstances[0];    
        boxMeshInst.setInstancing(vertexBuffer);
    </script>
</body>
</html>
